package com.example.controller;

import com.example.config.ConnectionPool;
import com.example.mapper.ResumeMapper;
import com.example.pojo.TbResume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ResumeController {

    @Autowired
    private ConnectionPool connectionPool;

    @RequestMapping("/")
    private String index(ModelMap map, HttpServletRequest request){
        map.addAttribute("serverName",request.getServerName());
        map.addAttribute("serverPort",request.getServerPort());
        return "index";
    }

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping(value = "/signup",method = {RequestMethod.POST})
    public String signup(HttpSession session, String username, String password){
        if ("admin".equals(username) && "admin".equals(password)){
            session.setAttribute("username","admin");
            session.setAttribute("password","admin");
            return "redirect:/list";
        }
        return "redirect:/login";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("username");
        session.removeAttribute("password");
        return "redirect:/login";
    }

    @RequestMapping("/list")
    public String list(ModelMap map){
        return "list";
    }


    @RequestMapping("/getListData")
    @ResponseBody
    public List<TbResume> getListData(){
        return connectionPool.getMapper().findAll();
    }

    @RequestMapping("/add")
    public String add(TbResume tbResume){
        connectionPool.getMapper().insert(tbResume);
        return "redirect:/list";
    }

    @RequestMapping("/updateById")
    public String updateById(TbResume tbResume){
        connectionPool.getMapper().update(tbResume);
        return "redirect:/list";
    }

    @RequestMapping("/delete")
    public String  delete(Integer id){
        connectionPool.getMapper().deleteById(id);
        return "redirect:/list";
    }
}
